package com.dianning.web.taglib;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.*;

import javax.servlet.jsp.PageContext;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.components.Component;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.dianning.comm.Constant;
import com.opensymphony.xwork2.util.ValueStack;

public class WebInfo extends Component {

	private PageContext pageContext;

	private String type;

	public WebInfo(ValueStack stack, PageContext pageContext) {
		super(stack);
		this.pageContext = pageContext;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public boolean start(Writer writer) {
		boolean result = super.start(writer);
		WebApplicationContext wc = WebApplicationContextUtils.getWebApplicationContext(this.pageContext
				.getServletContext());
		StringBuffer sb = new StringBuffer();
		try {
			if (this.getType().equalsIgnoreCase("area")) {
				sb.append(Constant.AREA_NAME);
			}
			
			if (this.getType().equalsIgnoreCase("poweredby")) {
				sb.append(" - ");
				sb.append(Constant.AREA_NAME);
				sb.append("点宁网");//点宁分类信息网
			}
			if (this.getType().equalsIgnoreCase("link")) {
				String path = Constant.ROOTPATH + "/link/links.txt";
				File f = new File(path);
				if(f.exists()){
					String links = FileUtils.readFileToString(f, "GBK");
					sb.append(links);
				}else{
					sb.append("");
				}
			}
			writer.write(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

}
